﻿#include "fileselectdialog.h"
#include "ui_fileselectdialog.h"

FileSelectDialog::FileSelectDialog(QString filePath, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FileSelectDialog),
    m_filePath(filePath),
    m_itemPerPage(8)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal);
    initControl();
    initData();
    refreshUi();
    m_fileName.clear();
}

FileSelectDialog::~FileSelectDialog()
{
    delete ui;
}

void FileSelectDialog::initControl()
{

    m_itemBtnList.clear();
    m_itemBtnList.append(ui->button1);
    m_itemBtnList.append(ui->button2);
    m_itemBtnList.append(ui->button3);
    m_itemBtnList.append(ui->button4);
    m_itemBtnList.append(ui->button5);
    m_itemBtnList.append(ui->button6);
    m_itemBtnList.append(ui->button7);
    m_itemBtnList.append(ui->button8);


    //根据不同分辨率设置控件的位置和尺寸
    initResolution();
    initControlStyle();//初始化窗体控件样式
}

//初始化窗体控件为任意分辨率
void FileSelectDialog::initResolution()
{
    double factoryX = getFactoryX();
    double factoryY = getFactoryY();

    this->resize(GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
    ui->frameBack->setGeometry(0*factoryX,0*factoryY,GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);

    for(int i = 0; i < m_itemPerPage; i++)//白色按下的效果
    {
        m_itemBtnList[i]->setGeometry(153*factoryX,(96 + i * 53)*factoryY,718*factoryX,53*factoryY);
        QString style1 = "QPushButton{outline: none;border:0px;border-radius:" + borderRadius() + "px;background-color:rgba(255, 255, 255, 0);color: rgb(255, 255, 255);}";
        m_itemBtnList[i]->setStyleSheet(style1);
        m_itemBtnList[i]->setTopImage(NULL);
    }

    ui->framePageBtn->setGeometry(0*factoryX,(GLB_SCR_HEIGHT-GLB_LBUT_HEIGHT-GLB_EDGE_WIGHT)*factoryY,GLB_SCR_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->labelPage->setGeometry(GLB_EDGE_WIGHT*factoryX,(GLB_SCR_HEIGHT-GLB_TEXT_HEIGHT-GLB_EDGE_WIGHT)*factoryY,(GLB_SCR_WIGHT/3)*factoryX,GLB_TEXT_HEIGHT*factoryY);
    ui->buttonPgUp->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*3+GLB_LBUT_WIGHT*4))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonPgDn->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*2+GLB_LBUT_WIGHT*3))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonOk->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*1+GLB_LBUT_WIGHT*2))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonCancel->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*0+GLB_LBUT_WIGHT*1))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
}

void FileSelectDialog::initControlStyle()
{
    SetStyle setControlStyle;
    setControlStyle.setUiName(this->parent()->objectName());
    //背景图
    QString frameBackImgPath = setControlStyle.getSharedStyleSheet();
    ui->frameBack->setStyleSheet(frameBackImgPath);


    //条目
    for(int i = 0; i < m_itemPerPage; i++)
    {
        m_itemBtnList[i]->setLabelFont(fontSize_M());
        connect(m_itemBtnList[i], SIGNAL(clicked(bool)), this, SLOT(btnclicked()));
    }

    //四个按钮样式表一样
    ui->buttonPgUp->setStyleSheet(confirmIconStyle());
    ui->buttonPgUp->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgUp->objectName()),12);

    ui->buttonPgDn->setStyleSheet(confirmIconStyle());
    ui->buttonPgDn->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgDn->objectName()),12);

    ui->buttonOk->setStyleSheet(confirmIconStyle());
    ui->buttonOk->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonOk->objectName()),12);

    ui->buttonCancel->setStyleSheet(confirmIconStyle());
    ui->buttonCancel->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonCancel->objectName()),12);
}

void FileSelectDialog::refreshUi()
{
    //清空全部按钮数据
    foreach (QPushButton* button, m_itemBtnList) {
        button->setText("");
        button->setEnabled(false);
        button->setVisible(false);
    }

    //计算页码范围
    m_pageNums =   m_fileNameList.size() / m_itemPerPage;//向上取整 总页数
    m_pageNums = (m_fileNameList.size() % m_itemPerPage == 0) ? m_pageNums : m_pageNums + 1;

    if (m_curPages > m_pageNums)// 当前页
    {
        m_curPages = m_pageNums;
    }
    if (m_curPages < 1)
    {
        m_curPages = 1;
    }
//    qDebug()<<"m_pageNums" <<m_pageNums;
//    qDebug()<<"m_curPages" <<m_curPages;

    //显示已存在的数据
    for(int i = 0; i < m_itemPerPage; i++){
        int index = (m_curPages - 1) * m_itemPerPage + i;
//        qDebug() << index;
        if(index > m_fileNameList.size() - 1)
        {
            break;
        }
        else{
            QString str = m_fileNameList[index];
            m_itemBtnList[i]->setText(str);
            m_itemBtnList[i]->setEnabled(true);
            m_itemBtnList[i]->setVisible(true);
        }
    }

    // 页信息
    QString str = tr("pageNum: ") + QString("%1/%2").arg(m_curPages).arg(m_pageNums);//页数:
    ui->labelPage->setText(str);
    ui->labelPage->setStyleSheet(commonTextColour());

}

void FileSelectDialog::initData()
{
    m_fileNameList.clear();
    QDir dir(m_filePath);
    m_curPages = 0;
    m_pageNums = 0;
    // 打开文件所在的路径
    if(!dir.exists())
    {
        qDebug() << "dirpath not exist, name=" << m_filePath;
        dir.mkdir(m_filePath);//创建花样文件夹
    }
    dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);  // 设置类型过滤器
    dir.setSorting(QDir::DirsFirst | QDir::IgnoreCase);  // 设置排序方式

    QFileInfoList fileList = dir.entryInfoList();
    int count = fileList.count();
    for(int i = 0; i < count ; i++){
        QFileInfo fileInfo = fileList[i];
        if(fileInfo.suffix().toUpper() == "HS"){
            QString str = fileInfo.fileName();
            m_fileNameList.append(str);
            qDebug()<<fileInfo.fileName();
        }
    }
}

void FileSelectDialog::btnclicked()
{
    MyButton* pBtn = (MyButton*)this->sender();
    qDebug()<< pBtn->text();

    for(int i = 0; i < m_itemPerPage; i++)//白色按下的效果
    {
        QString style1 = "QPushButton{outline: none;border:0px;border-radius:" + borderRadius() + "px;background-color:rgba(255, 255, 255, 0);color: rgb(255, 255, 255);}";
        m_itemBtnList[i]->setStyleSheet(style1);
    }
    QString style1 = "QPushButton{outline: none;border:0px;border-radius:" + borderRadius() + "px;background-color:rgba(255, 255, 255, 50);color: rgb(255, 255, 255);}";
    pBtn->setStyleSheet(style1);
    m_fileName = pBtn->text();
}

QString FileSelectDialog::getFileName()
{
    return m_fileName;
}

void FileSelectDialog::on_buttonOk_clicked()
{
    done(1);
}

void FileSelectDialog::on_buttonCancel_clicked()
{
    done(0);
}

void FileSelectDialog::on_buttonPgDn_clicked()
{
    m_curPages++;
    refreshUi();
}

void FileSelectDialog::on_buttonPgUp_clicked()
{
    m_curPages--;
    refreshUi();
}
